package com.cjjd.c.feature.home.chekin.select_detail;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.base.recycler.RecyclerAdapter;
import com.cjjd.c.base.recycler.ViewHolder;
import com.cjjd.c.bean.CheckInDetailBean;
import com.cjjd.c.bean.SelectHomeDetailItemBean;
import com.cjjd.c.feature.home.chekin.select_people.SelectPeopleActivity;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.ToastUtil;
import com.tencent.mm.opensdk.utils.Log;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/7.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class SelectHomeHorizontalAdapter extends RecyclerAdapter {
    private int from;

    List<CheckInDetailBean.HotelFloor> hotelFloorList = new ArrayList<>();
    // 楼栋
    private ArrayList<CheckInDetailBean.HotelFloor.HotelItemFloorItem> buildItemList;
    // 楼层
    private ArrayList<CheckInDetailBean.HotelFloor.HotelItemFloorItem> floorItemList;

    // floor 的adapter
    private SelectHomeHorizontalAdapter selectHomeHorizontalAdapter2;

    public SelectHomeHorizontalAdapter(Context context, List dataList, int layoutId, int from) {
        super(context, dataList, layoutId);
        this.from = from;
        LogUtil.e("xyd", "from:" + from);
    }

    @Override
    public void convert(final ViewHolder holder, Object itemData, final int position) {

        if (from == SelectHomeActivity.from_query_build) {
            final CheckInDetailBean.HotelFloor.HotelItemFloorItem itemBean = (CheckInDetailBean.HotelFloor.HotelItemFloorItem) itemData;
            CheckBox checkBox = holder.getView(R.id.checkbox_home_num);
            checkBox.setText(itemBean.name);
            checkBox.setChecked(itemBean.isSelected);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (CheckInDetailBean.HotelFloor.HotelItemFloorItem itemBean : (List<CheckInDetailBean.HotelFloor.HotelItemFloorItem>) mDataList) {
                        // 先遍历把所有按钮设置为未选中
                        itemBean.isSelected = false;
                    }
                    // 后设置本条为选中
                    itemBean.isSelected = true;
                    notifyDataSetChanged();

                    if (floorItemList.size() > 0) {
                        floorItemList.clear();
                    }
                    floorItemList.addAll(hotelFloorList.get(position).floor);
                    selectHomeHorizontalAdapter2.notifyDataSetChanged();
                }
            });

        }


        if (from == SelectHomeActivity.from_query_floor) {
            final CheckInDetailBean.HotelFloor.HotelItemFloorItem itemBean = (CheckInDetailBean.HotelFloor.HotelItemFloorItem) itemData;
            CheckBox checkBox = holder.getView(R.id.checkbox_home_num);
            checkBox.setText(itemBean.name);
            checkBox.setChecked(itemBean.isSelected);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (CheckInDetailBean.HotelFloor.HotelItemFloorItem itemBean : (List<CheckInDetailBean.HotelFloor.HotelItemFloorItem>) mDataList) {
                        // 先遍历把所有按钮设置为未选中
                        itemBean.isSelected = false;
                    }
                    // 后设置本条为选中
                    itemBean.isSelected = true;
                    notifyDataSetChanged();
                    EventBus.getDefault().post(EventBusKeys.hadSelectQuery);
                }
            });

        }

        if (from == SelectHomeActivity.from_result) {
            //TODO dada 缺少代码
            final SelectHomeDetailItemBean itemBeanitemData = (SelectHomeDetailItemBean) itemData;
            CheckBox checkBox = holder.getView(R.id.checkbox_home_num);
            checkBox.setText(itemBeanitemData.roomNo);
            checkBox.setChecked(itemBeanitemData.isSelected());
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (select_type == SelectPeopleActivity.SELECT_MULTI) {
                        // 多选

                        // 已经选择的房间数
                        int selectHomeNum = 0;
                        for (SelectHomeDetailItemBean itemBean : (List<SelectHomeDetailItemBean>) mDataList) {
                            if (itemBean.isSelected()) selectHomeNum++;
                        }
                        if (!itemBeanitemData.isSelected() && selectHomeNum < selectNum) {
                            itemBeanitemData.setSelected(true);
                        } else if (itemBeanitemData.isSelected()) {
                            itemBeanitemData.setSelected(false);
                        } else if (!itemBeanitemData.isSelected() && selectHomeNum == selectNum) {
                            ToastUtil.showToast("最多只能选择" + selectNum + "个房间。");
                        }
                        notifyDataSetChanged();
                    } else {
                        // 单选    先设置所有选项为未选中，再设置其中一项为选中
                        for (SelectHomeDetailItemBean itemBean : (List<SelectHomeDetailItemBean>) mDataList) {
                            itemBean.setSelected(false);
                        }
                        itemBeanitemData.setSelected(true);
                        Log.e("xyd", "position = " + position);
                        notifyDataSetChanged();
                    }

                }
            });
        }

    }


    public void setAllData(List<CheckInDetailBean.HotelFloor> hotelFloorList,
                           ArrayList<CheckInDetailBean.HotelFloor.HotelItemFloorItem> floorItemList,
                           SelectHomeHorizontalAdapter selectHomeHorizontalAdapter2) {
        this.hotelFloorList = hotelFloorList;
        this.floorItemList = floorItemList;
        this.selectHomeHorizontalAdapter2 = selectHomeHorizontalAdapter2;


    }

    private int select_type;
    private int selectNum;

    public void setSelectType(int select_type) {
        this.select_type = select_type;
    }

    public void setSelectedTravellerNum(int selectNum) {
        this.selectNum = selectNum;
    }
}
